package com.cn.yugan.Engine.Http;

import android.content.Context;
import android.content.SharedPreferences;

import com.cn.yugan.Utils.LogUtils;

import org.apache.http.Header;

/**
 * Created by Rover on 2016/10/10 .
 * Email 357319517@qq.com
 */
public class Cookies {
    private Header[] headers;
    private Context context;
    private SharedPreferences sp;


    public Cookies(Context context, Header[] headers) {
        this.headers = headers;
        this.context = context;
        sp = context.getSharedPreferences("Session", context.MODE_PRIVATE);
        inShared();
    }

    public void addcookie(Header[] headers) {
        this.headers = headers;
        inShared();
    }

    private void inShared() {
        SharedPreferences.Editor editor = sp.edit();
        for (int i = 0; i < headers.length; i++) {
            LogUtils.d(headers[i].toString());
            if (headers[i].toString().contains("Set-Cookie")) {
                String Cookie = headers[i].toString().replace("Set-Cookie:", "");
                if (Cookie.contains("PHPSESSID")) {
                    editor.putString("phpsessid", Cookie.trim());
                } else if (Cookie.contains("[uid]")) {
                    editor.putString("uid", Cookie.trim());
                } else if (Cookie.contains("[intro]")) {
                    editor.putString("intro", Cookie.trim());
                } else if (Cookie.contains("[name]")) {
                    editor.putString("name", Cookie.trim());
                } else if (Cookie.contains("[token]")) {
                    editor.putString("token", Cookie.trim());
                } else if (Cookie.contains("[img]")) {
                    editor.putString("img", Cookie.trim());
                } else if (Cookie.contains("_gat")) {
                    editor.putString("_gat", Cookie.trim());
                } else if (Cookie.contains("_ga")) {
                    editor.putString("_ga", Cookie.trim());
                }
            }
        }
        editor.commit();
    }
}